package com.itheima.edu.info.manager.system.controller;

import com.itheima.edu.info.manager.system.domain.Teacher;
import com.itheima.edu.info.manager.system.service.TeacherService2;

import java.util.Scanner;

/**
 * 操作老师时与用户交互的类
 * <p>
 * 有System.in
 * System.out
 */
public class TeacherController2 {

    private TeacherService2 service = new TeacherService2();
    //键盘录入各种信息
    private Scanner sc = new Scanner(System.in);

    /**
     * 各种操作老师
     * 人 去选择
     */
    public void start() {
        //Scanner sc = new Scanner(System.in);
        teacherloop:
        while (true) {
            System.out.println("--------欢迎来到 <老师> 管理系统--------");
            System.out.println("请输入您的选择: 1.添加老师  2.删除老师  3.修改老师  4.查看老师   5.退出");
            int choice = sc.nextInt();
            switch (choice) {
                case 1:
                    //System.out.println("添加");
                    addTeacher();
                    break;
                case 2:
                    //System.out.println("删除");
                    deleteTeacherById();
                    break;
                case 3:
                    //System.out.println("修改");
                    updateTeacher();
                    break;
                case 4:
                    //System.out.println("查询");
                    findAllTeachers();
                    break;
                case 5:
                    System.out.println("谢谢使用老师管理系统");
                    // return;
                    break teacherloop;
                default:
                    System.out.println("输入有误");
                    break;

            }

        }

    }

    /**
     * 修改老师信息
     */
    public void updateTeacher() {
        String id = getTeacherId();
        Teacher teacher = getTeacher(id);
        //调用servcie 添加方法 获取添加的结果
        service.updateTeacher(id,teacher);
        System.out.println("修改成功");
    }

    /**
     * 根据录入的学号删除老师
     */
    public void deleteTeacherById() {
      /*  //1.servce 调用查询所有老师
        Teacher[] stus = service.findAllTeachers();
        //2.判断是否为null
        if (stus == null) {
            System.out.println("没有数据, 请先添加");
            return;
        }*/
        String id = getTeacherId();

        service.deleteTeacherById(id);
        System.out.println("删除成功");

    }

    public String getTeacherId() {
        String id;
        while(true){
            //1.录入学号
            System.out.println("请输入要的学号");
           id= sc.next();
            /*//抽方法以后这个不能这样处理了
            if("0".equals(id)){
                break;
            }*/
            //2.判断是否存在
            boolean exits = service.isExits(id);
            //存在 删除  调用service 删除方法
            if(exits){

                break;
            }else {//不存在重新输入
                System.out.println("学号不存在,请重新输入,或输入0退出");
            }

        }
        return id;
    }

    /**
     * 查询所有老师
     */
    public void findAllTeachers() {
        //1.servce 调用查询所有老师
        Teacher[] stus = service.findAllTeachers();
        //2.判断是否为null
        if (stus == null) {
            System.out.println("没有数据, 请先添加");
            return;
        }
        //3.有, 打印表头
        System.out.println("学号\t姓名\t年龄\t生日");
        //4.循环数组 打印数据
        for (int i = 0; i < stus.length; i++) {
            //stus[i]
            Teacher s = stus[i];
            if (s != null) {
                System.out.println(s.getId() + "\t" + s.getName() + "\t" + s.getAge() + "岁\t" + s.getBirthday());

            }

        }
    }

    /**
     * controller 添加老师方法
     * <p>
     * 键盘录入老师信息
     * 给用户提示
     */
    public void addTeacher() {


        System.out.println("请输入学号");
        //判断学号是否存在
        String id;
        while (true) {
            id = sc.next();
            boolean result = service.isExits(id);
            if (result) {
                System.out.println("您输入的学号已经存在, 请重新输入");
            } else {
                break;
            }
        }

        Teacher teacher = getTeacher(id);
        //调用servcie 添加方法 获取添加的结果

        boolean flag = service.addTeacher(teacher);
        //判断结果给用户提示
        if (flag) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");

        }
    }

    public Teacher getTeacher(String id) {
        System.out.println("请输入姓名");
        String name = sc.next();
        System.out.println("请输入年龄");
        int age = sc.nextInt();
        System.out.println("请输入生日");
        String birthday = sc.next();
        //创建老师对象 封装数据
        Teacher teacher = new Teacher();
        teacher.setId(id);
        teacher.setName(name);
        teacher.setAge(age);
        teacher.setBirthday(birthday);
        return teacher;
    }
}
